package core;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import util.DB;

public class LogDAO {

    private static Log makeLog(ResultSet rs, boolean includeBody) throws Exception {
        Log obj= new Log();
        obj.setId(rs.getInt("Id"));
        obj.setUserId(rs.getInt("userId"));
        obj.setIP(rs.getString("IP"));
        obj.setURL(rs.getString("URL"));
        obj.setCategory(rs.getString("category"));
        obj.setWriteTime(rs.getTimestamp("writeTime"));
        if (includeBody) obj.setBody(rs.getString("body"));
        obj.setName(rs.getString("name"));
        return obj;
    }

    public static ArrayList<Log> selectPage(int currentPage, int pageSize, int order, int srchType, String srchText) throws Exception {
        String sql = "EXEC logSelectPage ?, ?, ?, ?, ?";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, currentPage);
            stmt.setInt(2, pageSize);
            stmt.setInt(3, order);
            stmt.setInt(4, srchType);
            stmt.setString(5, srchText);
            try (ResultSet rs = stmt.executeQuery()) {
                ArrayList<Log> list = new ArrayList<Log>();
                while (rs.next()) list.add(makeLog(rs, false));
                return list;
            }
        }
    }

    public static int getRecordCount(int srchType, String srchText) throws Exception {
        String sql = "EXEC logRecordCount ?, ?";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, srchType);
            stmt.setString(2, srchText);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next() ? rs.getInt(1) : 0;
            }
        }
    }

    public static void delete(int[] idList) throws Exception {
        String sql = "DELETE FROM [Log] WHERE [Id] = ?";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            for (int id : idList) {
                stmt.setInt(1, id);
                stmt.executeUpdate();
            }
        }
    }

    public static Log selectById(int id) throws Exception {
        String sql = "EXEC logSelectById ?";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next() ? makeLog(rs, true) : null;
            }
        }
    }

    public static void insert(int userId, String IP, String URL, String category, String message) {
        String sql = "INSERT [Log] ([userId], [IP], [URL], [category], [writeTime], [body]) VALUES (?,?,?,?,GETDATE(),?)";
        try (Connection con = DB.getConnection();
             PreparedStatement stmt = con.prepareStatement(sql)) {
            if (userId > 0) stmt.setInt(1, userId);
            else stmt.setNull(1, java.sql.Types.INTEGER);
            stmt.setString(2, IP);
            stmt.setString(3, URL);
            stmt.setString(4, category);
            stmt.setString(5, message);
            stmt.executeUpdate();
        } catch (Exception e) {
        }
    }

}
